<?php

namespace App\V3\V3_1\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class MaterielTypeModel extends Model
{
    use HasFactory;

    protected $table = 'materiel_type';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    //获取指定商品详细信息
    static public function info($id)
    {
        return self::select(
            'a.id',
            'a.materiel_name',
            'a.type_name',
            'a.materiel_img',
            'a.materiel_no',
            'a.materiel_class_id',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.lower_selling_price',
            'a.is_album',
            'a.materiel_unit',
            'a.lower_selling_price',
            'b.class_name as sec_class_name',
            'b.class_no as sec_class_no',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.is_use_serial',
            'a.is_product_materiel',
            'product_use_tag',
            'a.bar_code'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where('a.id', $id)
            ->where('a.delete_flag', 0)
            ->with('content', function($query) {
                $query->select(
                    'materiel_product_content.id',
                    'materiel_product_content.type_id',
                    'a.materiel_name',
                    'a.type_name',
                    'a.materiel_img',
                    'a.materiel_no',
                    'product_id',
                    'a.selling_price',
                    'materiel_product_content.num',
                    'materiel_product_content.price',
                    'materiel_product_content.total_price',
                )
                    ->leftJoin('materiel_type as a', 'a.id', '=', 'materiel_product_content.type_id');
            })
            ->first();
    }

    //物料类型列表
    static public function getMaterielTypeList($parentId, $condition, $keyword, $data)
    {
        return StorageManageModel::select(
            'b.*',
            'c.id as s_class_id',
            'c.class_name as s_class_name',
            'c.class_no as s_class_no',
            'd.class_name as f_class_name',
            'd.class_no as f_class_no',
            'd.id as f_class_id',
            'a.in_storage_num',
            'a.storage_id',
            'e.storage_name',
            'b.length',
            'b.width',
            'b.height',
            'b.weight',
            'b.attribute',
            'b.cost',
            'a.shelf_id',
            'a.location_id',
            's.shelf_name',
            'l.location_name',
        )
            ->from('storage_manage as a')
            ->leftJoin('materiel_type AS b', 'b.id', '=', 'a.type_id')
            ->leftJoin('materiel_class AS c', 'c.id', '=', 'b.materiel_class_id')
            ->leftJoin('materiel_class AS d', 'd.id', '=', 'c.parent_id')
            ->leftJoin('storage as e', 'e.id', '=', 'a.storage_id')
            ->leftJoin('storage_shelf as s','a.shelf_id','=','s.id')
            ->leftJoin('storage_shelf_location as l','a.location_id','=','l.id')

            ->where('a.main_id', $parentId)
            ->where('a.delete_flag', 0)
            ->where($condition)
            ->where(function($query) use ($keyword) {
                $query->orWhere('b.materiel_name', 'like', '%'.$keyword.'%');
                $query->orWhere('b.type_name', 'like', '%'.$keyword.'%');
                $query->orWhere('b.materiel_no', 'like', '%'.$keyword.'%');
                $query->orWhere('c.class_name', 'like', '%'.$keyword.'%');
                $query->orWhere('d.class_name', 'like', '%'.$keyword.'%');
            })
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
//        return StorageManageModel::select(
//            'b.*',
//            'in_storage_num'
//        )
//            ->from('storage_manage as a')
//            ->leftJoin('materiel_type AS b', 'b.id', '=', 'a.type_id')
//            ->leftJoin('materiel_class AS c', 'c.id', '=', 'b.materiel_class_id')
//            ->leftJoin('materiel_class AS d', 'd.id', '=', 'c.parent_id')
//            ->where('a.main_id', $parentId)
//            ->where('a.delete_flag', 0)
//            ->where($condition)
//            ->where(function($query) use ($keyword) {
//                $query->orWhere('a.materiel_name', 'like', '%'.$keyword.'%');
//                $query->orWhere('a.type_name', 'like', '%'.$keyword.'%');
//                $query->orWhere('a.materiel_no', 'like', '%'.$keyword.'%');
//                $query->orWhere('b.class_name', 'like', '%'.$keyword.'%');
//                $query->orWhere('c.class_name', 'like', '%'.$keyword.'%');
//            })
//            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
    }

    //校验物料分类随机code
    static public function checkMaterielTypeCode($materielNo, $parentId)
    {
        return self::where([
                'materiel_no' => $materielNo,
                'main_id' => $parentId,
                'delete_flag' => 0
            ])->first();
    }

    static public function getMainInfo($id)
    {
        return self::select(
            'a.id',
            'a.materiel_name',
            'a.type_name',
            'a.materiel_img',
            'a.materiel_no',
            'a.materiel_class_id',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.lower_selling_price',
            'a.is_album',
            'a.materiel_unit',
            'a.lower_selling_price',
            'b.class_name as sec_class_name',
            'b.class_no as sec_class_no',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.is_use_serial',
            'a.is_product_materiel',
            'product_use_tag'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where('a.id', $id)
            ->where('a.delete_flag', 0)
            ->first();
    }

    public function content()
    {
        return self::hasMany(MaterielProductContentModel::class, 'product_id', 'id');
    }

    //验证物料类型信息
    static public function checkMaterielInfo($data)
    {
        return self::where([
                        'materiel_name' => $data['materiel_name'],
                        'type_name' => $data['type_name'],
                        'main_id' => $data['main_id'],
                        'delete_flag' => 0,
                    ])
                    ->first();
    }

    static public function getInfo($id)
    {
        return self::where('id', $id)->with('content')->first()->toArray();
    }
}
